package com.nexgo.payment.trans.setting.terminalkey.iccimportkey;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.nexgo.payment.R;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.fragment.ProgressDialogFragment;
import com.nexgo.payment.ui.BaseActivity;
import com.nexgo.payment.util.ToastUtils;
import com.nexgo.payment.widget.TitleBuilder;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * IC卡导密钥界面
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年8月30日
 */
public class ICCardImportKeyActivity extends BaseActivity implements ICCardImportKeyContract.View {

    @BindView(R.id.et_key_no)
    EditText etKeySn;
    @BindView(R.id.et_card_password)
    EditText etCardPassword;
    @BindView(R.id.et_store_password)
    EditText etStorePassword;

    private ICCardImportKeyContract.Presenter mPresenter;
    private ProgressDialogFragment mDialogFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_iccard_import_key);
        ButterKnife.bind(this);

        initTitle();

        new ICCardImportKeyPresenter(getApplicationContext(), this, Repository.getInstance());
    }

    /**
     * 初始化标题
     */
    private void initTitle() {
        new TitleBuilder(this)
                .setExternalTitleBgRes(R.drawable.bg_blue_icon)
                .setLeftImage(R.drawable.arrow_icon)
                .setTitleText(getString(R.string.ic_card_import_key))
                .build();
    }

    @OnClick({R.id.title_iv_left, R.id.btn_confirm})
    public void onClicked(View view) {
        switch (view.getId()) {
            case R.id.title_iv_left:
                finish();
                break;
            case R.id.btn_confirm:
                mPresenter.confirm(etKeySn.getText().toString().trim(),
                        etCardPassword.getText().toString().trim(),
                        etStorePassword.getText().toString().trim());
                break;
        }
    }

    @Override
    public void setPresenter(ICCardImportKeyContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showInputEmpty() {
        ToastUtils.showToast(R.string.input_can_no_be_empty);
    }

    @Override
    public void showMsg(String message) {
        if (mDialogFragment == null){
            mDialogFragment = new ProgressDialogFragment();
        }
        mDialogFragment.setTipTitle(getString(R.string.ic_card_import_key_));
        mDialogFragment.setTip(message);
        mDialogFragment.setCancelable(false);
        mDialogFragment.show(getSupportFragmentManager(),null);
    }

    @Override
    public void onDismiss() {
        if (mDialogFragment != null && mDialogFragment.getDialog() != null){
            Dialog dialog = mDialogFragment.getDialog();
            if (dialog.isShowing()) mDialogFragment.dismiss();
            mDialogFragment = null;
        }
    }

    @Override
    public void showErrMsg(String message) {
        ToastUtils.showToast(message);
    }

    @Override
    public void showSuccessful() {
        ToastUtils.showToast(R.string.import_key_success);
        finish();
    }

    @Override
    public void showFail() {
        ToastUtils.showToast(R.string.import_key_fail);
        finish();
    }
}
